package com.gexiaobao.pigeon.ui.fragment.setting.pattern;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.gexiaobao.pigeon.R;
import com.github.ihsg.patternlocker.OnPatternChangeListener;
import com.github.ihsg.patternlocker.PatternIndicatorView;
import com.github.ihsg.patternlocker.PatternLockerView;

import java.util.List;


public class PatternCheckingActivity extends AppCompatActivity {
    private PatternLockerView patternLockerView;
    private PatternIndicatorView patternIndicatorView;
    private TextView textMsg;
    private PatternHelper patternHelper;

    public static void startAction(Context context) {
        Intent intent = new Intent(context, PatternCheckingActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_default_pattern_checking);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
        this.patternIndicatorView = (PatternIndicatorView) findViewById(R.id.pattern_indicator_view);
        this.patternLockerView = (PatternLockerView) findViewById(R.id.pattern_lock_view);
        this.textMsg = (TextView) findViewById(R.id.text_msg);

        this.patternLockerView.setOnPatternChangedListener(new OnPatternChangeListener() {
            @Override
            public void onStart(PatternLockerView view) {
            }

            @Override
            public void onChange(PatternLockerView view, List<Integer> hitList) {
            }

            @Override
            public void onComplete(PatternLockerView view, List<Integer> hitList) {
                boolean isError = !isPatternOk(hitList);
                view.updateStatus(isError);
                patternIndicatorView.updateState(hitList, isError);
                updateMsg();
            }

            @Override
            public void onClear(PatternLockerView view) {

                Log.d("hello", "check error !!!!!!!");
                finishIfNeeded();
            }
        });

        this.textMsg.setText("请绘制解锁图案");
        this.patternHelper = new PatternHelper();
    }

    private boolean isPatternOk(List<Integer> hitList) {
        this.patternHelper.validateForChecking(hitList);
        return this.patternHelper.isOk();
    }

    private void updateMsg() {
        this.textMsg.setText(this.patternHelper.getMessage());
        this.textMsg.setTextColor(this.patternHelper.isOk() ?
                getResources().getColor(R.color.patternPrimary) :
                getResources().getColor(R.color.patternAccent));
    }

    private void finishIfNeeded() {
        if (this.patternHelper.isFinish()) {
            Log.d("hello", "pattern check status left times ======================== " + this.patternHelper.leftTimes());


            Intent intent = new Intent();
            if (this.patternHelper.leftTimes() != 4) {
                intent.putExtra("result", "true");
            } else {
                intent.putExtra("result", "false");
            }
            Log.d("hello", "pattern check status left times ======================== " + 111);
            setResult(AppCompatActivity.RESULT_OK, intent);
            finishActivity(0);
            finish();
        } else {
            Log.d("hello", "pattern check status left times !!!!= " + this.patternHelper.leftTimes());

        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        Intent intent = new Intent();
        intent.putExtra("result", "false");
        setResult(AppCompatActivity.RESULT_OK, intent);
        finishActivity(0);
        finish();
    }
}
